package com.tusuo.stds.ui.linkaccount;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ApiResult;
import com.tusuo.stds.ui.login.ThirdLoginUserInfo;
import com.tusuo.stds.ui.resetpassword.ResetPasswordActivity_;
import com.tusuo.stds.util.IntentUtils;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

/**
 * @author J.Huang
 * @version 1.0
 *
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(LinkAccountPresenter.class)
class LinkAccountPresenter extends MvpBasePresenter<LinkAccountView> {

    @Inject
    ApiHelper mApiHelper;

    private Context mContext;
    private String mErrMsgServer;

    private String mThirdLoginName = "";

    private Handler mHandlerShowErrMsg;
    private Subscriber<ApiResult> mLinkAccountSubscriber = null;

    LinkAccountPresenter(Context context) {
        mContext = context;
        mHandlerShowErrMsg = new Handler();
        mErrMsgServer = mContext.getResources().getString(R.string.err_msg_server);

        Injector.initLinkAccountPresenterComponent().inject(this);
    }

    @Override
    public void detachView(boolean retainInstance) {
        if (mHandlerShowErrMsg != null) {
            mHandlerShowErrMsg.removeCallbacksAndMessages(null);
        }
        if (mLinkAccountSubscriber != null) {
            mLinkAccountSubscriber.unsubscribe();
            mLinkAccountSubscriber = null;
        }

        super.detachView(retainInstance);
    }

    private Runnable showSloganRunnable = new Runnable() {
        @Override
        public void run() {
            if (getView() != null) {
                getView().showSlogan();
            }
        }
    };

    void showErrMsg(String text) {
        if (getView() != null) {
            getView().showErrMsg(text);
            mHandlerShowErrMsg.postDelayed(showSloganRunnable, 3000);
        }
    }

    void resetPassword() {
        mContext.startActivity(new Intent(mContext, ResetPasswordActivity_.class));
    }

  /*  void linkAccount(String mobileNo, String password, String openId, String platForm, String accessToken, String nickName) {
        mLinkAccountSubscriber = new LinkAccountSubscriber();
        mApiHelper.linkAccount(mLinkAccountSubscriber, mobileNo, password, openId, platForm, accessToken, nickName);
    }*/

    void linkAccount(String mobileNo, String password, ThirdLoginUserInfo thirdLoginUserInfo) {
        mLinkAccountSubscriber = new LinkAccountSubscriber(thirdLoginUserInfo);
        mApiHelper.linkAccount(mLinkAccountSubscriber, mobileNo, password,
                thirdLoginUserInfo.openId,
                thirdLoginUserInfo.platForm,
                thirdLoginUserInfo.accessToken,
                thirdLoginUserInfo.nickName,
                thirdLoginUserInfo.userIcon);
    }

    private class LinkAccountSubscriber extends Subscriber<ApiResult> {
        private ThirdLoginUserInfo thirdLoginUserInfo;

        public LinkAccountSubscriber(ThirdLoginUserInfo thirdLoginUserInfo) {
            this.thirdLoginUserInfo = thirdLoginUserInfo;
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            showErrMsg(mErrMsgServer);
        }

        @Override
        public void onNext(final ApiResult result) {
            if (result.checkState(mContext)) {
                // Login successfully!
//                IntentUtils.toMainActivity(mContext);
                IntentUtils.toBindNewMainActivity(mContext,thirdLoginUserInfo,false);
            } else {

                showErrMsg(result.ErrorMessage);
            }
        }
    }
}
